package com.ppp.poi.bean.dto;

import com.ppp.poi.common.exception.AppSimpleException;
import lombok.Data;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

/**
 * @author Wu Jin
 * @create 2020-01-20
 */
@Data
public class PoiParam {

  private Integer userId;
  private String mobile;
  private String email;
  private String citycode;
  private String lng1;
  private String lat1;
  private String lng2;
  private String lat2;
  private String headers;
  private String keyword;
  private String polygon;
  private String types;
  private MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();

  private Boolean isSqu = null;

  public Boolean getIsSqu() {
    if (isSqu == null) {
      if (StringUtils.isEmpty(polygon) || !polygon.contains("|")) {
        throw new AppSimpleException("polygon值不符合规范");
      }
      String[] strs = StringUtils.split(polygon, "|");
      if (strs.length == 2) {
        String[] s1 = StringUtils.split(strs[0], ",");
        String[] s2 = StringUtils.split(strs[1], ",");
        if (s1.length == 2 && s2.length == 2) {
          isSqu = true;
          lng1 = s1[0];
          lat1 = s1[1];
          lng2 = s2[0];
          lat2 = s2[1];
        } else {
          throw new AppSimpleException("polygon值不符合规范");
        }
      } else if (strs.length > 2 && strs[0].equals(strs[strs.length - 1])) {
        isSqu = false;
      } else {
        throw new AppSimpleException("polygon值不符合规范");
      }

    }
    return isSqu;
  }

  public String getLng1() {
    if (getIsSqu()) {
      return lng1;
    } else {
      return "";
    }
  }

  public String getLat1() {
    if (getIsSqu()) {
      return lat1;
    } else {
      return "";
    }
  }

  public String getLng2() {
    if (getIsSqu()) {
      return lng2;
    } else {
      return "";
    }
  }

  public String getLat2() {
    if (getIsSqu()) {
      return lat2;
    } else {
      return "";
    }
  }

  public String getCitycode() {
    return StringUtils.isEmpty(citycode) ? "" : citycode;
  }

  public String getKeyword() {
    return StringUtils.isEmpty(keyword) ? "" : keyword;
  }

  public String getTypes() {
    return StringUtils.isEmpty(types) ? "" : types;
  }

  @Override
  public String toString() {
    return "PoiParam{" +
        "userId=" + userId +
        ", mobile='" + mobile + '\'' +
        ", email='" + email + '\'' +
        ", citycode='" + citycode + '\'' +
        ", headers='" + headers + '\'' +
        ", keyword='" + keyword + '\'' +
        ", polygon='" + polygon + '\'' +
        ", types='" + types + '\'' +
        '}';
  }

}
